/**
 * 2013 2013-7-16
 */
package com.surfilter.platform.base.model;

import java.util.Date;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.surfilter.platform.base.util.ParaUtil;

/**
 * 
 * 查询条件模型基类 ，封装了基本条件信息，排序信息
 * 
 * 提供静态方法用于从request中直接获取实例
 */
public class ConditionBase extends ModelBase{
	private Date startDate;
	
	private Date endDate;
	
	private boolean dataSort;
	
	private String multSortStr;
	
	private String sortName;
	
	private String sortType;
	
	/**
	 * 从request中直接获取查询条件
	 * @param request
	 * @param clz
	 * @return
	 */
	public static  <T extends ConditionBase> T getCondition(HttpServletRequest request,Class<T> clz){
		T t = ParaUtil.getBean(request, clz);
		String sortStr = request.getParameter("sort");
		if(!StringUtils.isEmpty(sortStr)){
			t.setDataSort(true);
			JSONArray array = JSONArray.fromObject(sortStr);
			JSONObject obj = array.getJSONObject(0);
			t.setSortName(obj.getString("property"));
			t.setSortType(obj.getString("direction"));
			t.setMultSortStr(parseArrayToOrderString(array));
		}
		return t;
	}  
	
	private static String parseArrayToOrderString(JSONArray array){
		Iterator itr = array.iterator();
		StringBuffer target = new StringBuffer();
		int i = 0;
		while(itr.hasNext()){
			JSONObject obj = (JSONObject)itr.next();
			String sortProperty = obj.getString("property");
			String sortDirection = obj.getString("direction");
			if(i == 0){
				target.append(sortProperty + " " + sortDirection);
			}else{
				target.append("," + sortProperty + " " + sortDirection);
			}
			i ++;
		}
		return target.toString().equals("") ? null : target.toString();
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the dataSort
	 */
	public boolean isDataSort() {
		return dataSort;
	}

	/**
	 * @param dataSort the dataSort to set
	 */
	public void setDataSort(boolean dataSort) {
		this.dataSort = dataSort;
	}

	/**
	 * @return the multSortStr
	 */
	public String getMultSortStr() {
		return multSortStr;
	}

	/**
	 * @param multSortStr the multSortStr to set
	 */
	public void setMultSortStr(String multSortStr) {
		this.multSortStr = multSortStr;
	}

	/**
	 * @return the sortName
	 */
	public String getSortName() {
		return sortName;
	}

	/**
	 * @param sortName the sortName to set
	 */
	public void setSortName(String sortName) {
		this.sortName = sortName;
	}

	/**
	 * @return the sortType
	 */
	public String getSortType() {
		return sortType;
	}

	/**
	 * @param sortType the sortType to set
	 */
	public void setSortType(String sortType) {
		this.sortType = sortType;
	}
	
}
